<?php

namespace IDock\Insurance\HBIC\Request;

use IDock\Insurance\DAJIA\Response\InsuranceOrderQueryResponse;
use IDock\Insurance\HBIC\Bean\BASEPART;
use IDock\Insurance\HBIC\Bean\CAR;
use IDock\Insurance\HBIC\Bean\CARSHIPTAX;
use IDock\Insurance\HBIC\Bean\DEVICE;
use IDock\Insurance\HBIC\Bean\KIND;
use IDock\Insurance\HBIC\Bean\NOCAR;
use IDock\Insurance\HBIC\Bean\PERSONNEL;
use IDock\Insurance\HBIC\HBICEnums;
use IDock\Insurance\HBIC\Response\CommonResponse;
use IDock\Insurance\HBIC\Response\InsuranceConfirmResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class InsuranceCheckRequest extends AllHBICRequest
{
    public $requestType = 'A02';

    protected $response = InsuranceConfirmResponse::class;

    protected $urlTip = '转投保接口';

    protected $resourceType = 'BXT';

    protected $methodName = 'quoteToProposal';

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $param = [
            'resourceType' => $this->resourceType,
            'methodName' => $this->methodName,
            'BASEPART' => $this->getBasePart(),
            'INSUREDLIST' => [
                'INSURED' => $this->getInsured(),
                'APPLICANT' => $this->getApplicant(),
            ],
            'CAROWNERMESSAGE' => [
                'CAROWNERITEMPROVINCECODE' => '',
                'CAROWNERITEMPROVINCECNAME' => '',
                'CAROWNERITEMCITYCODE' => '',
                'CAROWNERITEMCITYCNAME' => '',
                'CAROWNERITEMDISTRICTCODE' => '',
                'CAROWNERITEMDISTRICTCNAME' => '',
            ],
//            'VIDEOMATERIALSLIST' => ['VIDEOMATERIALS' => $this->getImageList()]
        ];
        if($this->getEngageList()){
            $param['ENGAGELIST']  = ['ENGAGE' => $this->getEngageList()];
        }
        foreach ($param as $key => $val) {
            if (empty($val)) {
                unset($param[$key]);
            }
        }
        return $param;
    }

    public function getBasePart()
    {
        return [
            'QUOTATIONNO' => $this->visitor->getUnderwritingVisitor()->inquiryNo,
            'OUTSYNCTYPE' => '',
            'PACKAGECODE' => $this->visitor->getExtInfoVisitor()->argc['PACKAGECODE'] ?? '',
            'RISKTYPE' => $this->getInsuranceType(),
            'INPUTSYSCODE' => $this->visitor->getExtInfoVisitor()->argc['INPUTSYSCODE'] ?? '',
            'QUOTMPNNO' => '',
            'AGENTTEAM' => '',
            'AGENTNAME' => '',
            'AGENTMOBILE' => '',
            'USERNAME' => '',
            'USERMOBILE' => '',
        ];
    }

    private function getInsuranceType()
    {
        if (!empty($this->visitor->biStartData) && !empty($this->visitor->ciStartData)) {
            return 2;
        } else {
            if (!empty($this->visitor->ciStartData)) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public function getApplicant()
    {
        $applicantData = $this->visitor->getApplicantVisitor();
        $applicant = [
            'INSUREDFLAG' => '2',
            'INSUREDNAME' => $applicantData->name,
            'IDENTIFYNUMBER' => $applicantData->certificateNo,
            'IDENTIFYTYPE' => HBICEnums::$certificateTypeCode[$applicantData->certificateTypeCode],//关系人证件类型
            'MOBILE' => $applicantData->mobile,
            'EMAIL' => $applicantData->email,
            'POSTCODE' => $applicantData->postcode,//成都
            'INSUREDADRESS' => $applicantData->address,
            'COUNTRYCODE' => 'CN',
            'IDENTIFYNUMBERSTART' => $applicantData->certificateStartDate,
            'IDENTIFYNUMBEREND' => $applicantData->certificateEndDate,
            'ITEMPROVINCECODE' => $applicantData->provinceCode,//省份代码
            'ITEMPROVINCECNAME' => $applicantData->provinceName,
            'ITEMCITYCODE' => $applicantData->cityCode,
            'ITEMCITYCNAME' => $applicantData->cityName,
            'ITEMDISTRICTCODE' => $applicantData->areaCode,
            'ITEMDISTRICTCNAME' => $applicantData->areaName,
            'OCCUPATIONCODE' => '60',
            'INVOICEREQUEST' => '',
            'INSUREDTYPE' => $this->visitor->getVehicleVisitor()->attachNature == '01' ? '1' : '2',//关系人类型含义:1:个人;2:单位
            'TAXPAYERTYPE' => '',
            'TAXPAYERNUMBER' => '',
            'TAXADDRESS' => '',
            'TAXPHONE' => '',
            'ACCOUNTBANK' => '',
            'BANKACCOUNTNUMBER' => '',
            'CUSTOMERTYPE' => '00',//客户类型
            'CONTACTNAME' => '',
            'CONTACTIDENTIFYTYPE' => '',
            'CONTACTIDENTIFYNUMBER' => '',
            'CONTACTEXPIRATIONSTARTDATE' => '',
            'CONTACTEXPIRATIONENDDATE' => '',
            'CONTACTPHONE' => '',
            'STARLEVEL' => '',
            'OFFICEPHONE' => '',
            'REPRESENTATIVE' => '',
            'REPRESENTIDENTIFYNUMBER' => '',
        ];

        return $applicant;
    }

    public function getInsured()
    {
        $insuredData = $this->visitor->getInsuredVisitor();
        $insured = [
            'INSUREDFLAG' => '1',
            'INSUREDNAME' => $insuredData->name,
            'IDENTIFYNUMBER' => $insuredData->certificateNo,
            'IDENTIFYTYPE' => HBICEnums::$certificateTypeCode[$insuredData->certificateTypeCode],
            'MOBILE' => $insuredData->mobile,
            'EMAIL' => $insuredData->email,
            'POSTCODE' => $insuredData->postcode,
            'INSUREDADRESS' => $insuredData->address,
            'COUNTRYCODE' => 'CN',
            'IDENTIFYNUMBERSTART' => $insuredData->certificateStartDate,
            'IDENTIFYNUMBEREND' => $insuredData->certificateEndDate,
            'ITEMPROVINCECODE' => $insuredData->provinceCode,
            'ITEMPROVINCECNAME' => $insuredData->provinceName,
            'ITEMCITYCODE' => $insuredData->cityCode,
            'ITEMCITYCNAME' => $insuredData->cityName,
            'ITEMDISTRICTCODE' => $insuredData->areaCode,
            'ITEMDISTRICTCNAME' => $insuredData->areaName,
            'OCCUPATIONCODE' => '60',
            'CONTACTNAME' => '',
            'CONTACTIDENTIFYTYPE' => '',
            'CONTACTIDENTIFYNUMBER' => '',
            'CONTACTEXPIRATIONSTARTDATE' => '',
            'CONTACTEXPIRATIONENDDATE' => '',
            'STARLEVEL' => '',
            'OFFICEPHONE' => '',
            'CONTACTMOBILE' => '',
        ];
        return $insured;
    }

    public function getEngageList()
    {
        $engageList = [];
        $list = $this->visitor->getUnderwritingVisitor()->specialClausesList;
        foreach ($list as $key => $val) {
            /**@var SpecialClausesVisitor $val */
            $engageList[] = [
                'SERIALNO' => ($key + 1),
                'LINENO' => ($key + 1),
                'CLAUSECODE' => $val->clauseCode,
                'CLAUSECNAME' => $val->clauseName,
                'CLAUSECONTEXT' => $val->clauseContent,
                'RISKCODE' => $val->riskType == 'TCI' ? '0801' : '0808',
            ];
        }
        return $engageList;
    }

    public function getImageList()
    {
        $list = $this->visitor->getFileList();
        $list = [
            'VIDEO_NO' => '',
            'VIDEO_TYPE' => '',//影像类型
            'VIDEO_CODE' => '',//影像编码
            'VIDEO_CNAME' => '',//影像名称
        ];
        return $list;
    }

    public function getStaffValue(&$comCode)
    {
        $comCode = $this->visitor->getExtInfoVisitor()->argc['comCode'] ?? '';
    }
}
